#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "objectmodel.h"
#include "obja.h"

#include <QTimer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    auto model = new ObjectModel(this);
    for(int i = 0; i < 10; i++) {
        auto obja = new ObjA;

        obja->iv = i + 1;
        obja->fv = i / 3.1;
        obja->sv = QString("str%1").arg(i);
        model->append(obja);
    }

    ui->tableView->setModel(model);

    auto timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [model](){
        static int count = 0;

        auto row = count % model->rowCount();
        auto item = model->item(row);
        auto obja = qobject_cast<ObjA*>(item);
        if(obja != nullptr) {
            obja->iv = count;
            emit obja->ivChanged();

            obja->setFv(count + 1);
            item->setProperty("sv", count + 2);
        }

        count += 1;
    });

    timer->start(1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}
